/*
 * Copyright 2009 - Morten Udnæs
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.javabatch.petstore.fixtures;

import static fitnesse.util.ListUtility.list;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.javabatch.fileio.FieldError;
import com.javabatch.fileio.FileBuffer;
import com.javabatch.fileio.FileStore;
import com.javabatch.fileio.RecordBasedFileStore;
import com.javabatch.petstore.records.AnimalRecord;

/**
 * Simple file generator for use with fitnesse.
 * 
 * @author eco
 */
public class AnimalFileFixture {
    /** Customer file. */
    private static FileStore store;
    
    /** customer id. */
    private String id;
    
    /** Full name. */
    private String name;
   

    public void setId(String theId) {
        this.id = theId;
    }


    public void setName(String theName) {
        this.name = theName;
    }



    /** Constructor to use from test when closing file. */
    public AnimalFileFixture() {
    }

    public AnimalFileFixture(final String file, final String readWrite) {
        
        if (readWrite.equalsIgnoreCase("R")) {
            store = new RecordBasedFileStore(file,
                    RecordBasedFileStore.READ_ACCESS);
        } else {
            store = new RecordBasedFileStore(file,
                    RecordBasedFileStore.WRITE_ACCESS);
        }
        store.open();
    }


    /**
     * Write text to file.
     * 
     * @return true if ok
     */
    public String add() {
        FileBuffer outRecord = new FileBuffer();
        AnimalRecord.ANIMAL_NAME.write(outRecord, this.name);
        AnimalRecord.ANIMAL_GUID.write(outRecord, this.id);
    
        store.write(outRecord);
        return "";
    }
    
    public List<Object> query() {
        List<FieldError> errors = new ArrayList<FieldError>();
        List<Object> rows = new LinkedList<Object>();
        FileBuffer inputRecord = store.readNext();
        
        while (inputRecord != null) {
            List<Object> aRow = 
                    list(list("name", AnimalRecord.ANIMAL_NAME.read(inputRecord, errors).trim()),
                         list("id", AnimalRecord.ANIMAL_GUID.read(inputRecord, errors).replaceAll("^0*", "")));
                    
            if (errors.size() == 0) {
                rows.add(aRow);
            } else {
                for (FieldError error : errors) {
                    rows.add(list(list("id", error.toString()),
                            list("name", ""), list("address", ""), list(
                                    "postal code", "")));
                }
            }

            inputRecord = store.readNext();
        }

        return rows;
    }

    public static boolean close() {
        store.close();
        return true;
    }
}
